
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%-- 导入面包屑 --%>
<jsp:include page="../common/nav.jsp"></jsp:include>

<div class="wrapper wrapper-content animated fadeInRight">

	<%-- 导入提示信息框 --%>
    <c:if test="${hint!=null&&hint!=''}">
   		<jsp:include page="../common/alert/alert.jsp">
   			<jsp:param value="${hint}" name="alertType"/>
   			<jsp:param value="${alertMessage}" name="alertMessage"/>
   		</jsp:include>
    </c:if>

	<div class="row">
		<div class="col-lg-12">
			<div class="ibox ">
				<div class="ibox-title">
					<h4>期初余额设置</h4>
					<div class="ibox-tools">
					</div>
				</div>

				<div class="ibox-content">
					<form id="editForm" action="web/gLBalance/editGLBalance" method="post">
					
						<div class="form-group  row">
							<label class="col-sm-2 col-form-label"><span class="text-danger">*</span>编码</label>
							<div class="col-sm-10">
								<input id="balanceCode" name="balanceCode" type="text" class="form-control" value="${requestScope.gLBalance.balanceCode}">
							</div>
						</div>
						<div class="hr-line-dashed"></div>

						<div class="form-group  row">
							<label class="col-sm-2 col-form-label"><span class="text-danger">*</span>分类账簿</label>
							<div class="col-sm-10">
								<select class="form-control" name="ledgerCode" id="ledgerCode">
		                        	<option value="" selected="selected">请选择...</option>
		                        	<option value="${requestScope.glLedger.ledgerCode}">${requestScope.glLedger.ledgerName}</option>
		                        </select>
		                        <div id="startupTypeText" style="color: silver;"></div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						
						<div class="form-group  row">
							<label class="col-sm-2 col-form-label"><span class="text-danger">*</span>币种</label>
							<div class="col-sm-10">
								<select class="form-control" name="currencyCode" id="currencyCode">
		                        	<option value="" selected="selected">请选择...</option>
		                            <c:forEach items="${requestScope.currencyTypeMap}" var="currencyType">
		                        		<option value="${currencyType.key}">${currencyType.value}</option>
		                        	</c:forEach>
		                        </select>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						
						<div class="form-group  row">
							<label class="col-sm-2 col-form-label">会计期间</label>
							<div class="col-sm-10">
								<select class="form-control" name="periodName" id="periodName">
		                        	<option value="" selected="selected">请选择...</option>
		                            <c:forEach items="${requestScope.calendarPeriodList}" var="calendarPeriod">
		                        		<option value="${calendarPeriod.periodName}">${calendarPeriod.periodName}</option>
		                        	</c:forEach>
		                        </select>
		                        <div style="color: silver;">期初会计期间请选择启动总账的期间，如果账簿是年中启用，程序将自动调整</div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						
						<div class="form-group  row">
							<label class="col-sm-2 col-form-label"><span class="text-danger">*</span>会计科目结构</label>
							<div class="col-sm-10">
								<input name="subjectDesc" type="text" class="form-control" value="${requestScope.gLBalance.subjectDesc}" title="${requestScope.gLBalance.subjectDescTitle}">
								<span class="subjectDescTitle" style="font-size: 10px; color: silver; font-weight: normal;">${requestScope.gLBalance.subjectDescTitle}</span>
								<input name="subjectCode" type="hidden" class="form-control" value="${requestScope.gLBalance.subjectCode}">
								<br/><span id="subjectCodeSpan" style="color: red; display: none;">当前科目结构已录入期初，不能重复录入</span>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						
						<div class="form-group row">
	                        <label class="col-sm-2 col-form-label"><span class="text-danger">*</span>期初借方余额</label>
	                        <div class="col-sm-4">
	                        	<div class="input-group m-b">
		                        	<input id="endDr" name="endDr" type="text" class="form-control" value="${requestScope.gLBalance.endDr}">
		                        	<div class="input-group-append">
	                                	<span class="input-group-addon">(元)</span>
	                                </div>
                                </div>
	                        </div>
	                        
	                        <label class="col-sm-2 col-form-label"><span class="text-danger">*</span>期初贷方余额</label>
	                        <div class="col-sm-4">
	                        	<div class="input-group m-b">
		                        	<input id="endCr" name="endCr" type="text" class="form-control" value="${requestScope.gLBalance.endCr}">
		                        	<div class="input-group-append">
	                                	<span class="input-group-addon">(元)</span>
	                                </div>
                                </div>
	                        </div>
	                    </div>
	                    <div class="hr-line-dashed"></div>
	                    
	                    <div class="form-group row">
	                        <label class="col-sm-2 col-form-label"><span class="text-danger">*</span>年初借方余额</label>
	                        <div class="col-sm-4">
	                        	<div class="input-group m-b">
		                        	<input id="beginDr" name="beginDr" type="text" class="form-control" value="${requestScope.gLBalance.beginDr}">
		                        	<div class="input-group-append">
	                                	<span class="input-group-addon">(元)</span>
	                                </div>
                                </div>
	                        </div>
	                        
	                        <label class="col-sm-2 col-form-label"><span class="text-danger">*</span>年初贷方余额</label>
	                        <div class="col-sm-4">
	                        	<div class="input-group m-b">
		                        	<input id="beginCr" name="beginCr" type="text" class="form-control" value="${requestScope.gLBalance.beginCr}">
		                        	<div class="input-group-append">
	                                	<span class="input-group-addon">(元)</span>
	                                </div>
                                </div>
	                        </div>
	                    </div>
	                    <div class="hr-line-dashed"></div>

						<div class="form-group row">
							<div class="col-sm-12 col-sm-offset-2 text-right">
								<button class="btn btn-white btn-lg" type="button" onclick="window.location.href='web/gLBalance/getGLBalanceList'">返回</button>&nbsp;
								<c:if test="${requestScope.actualNum==0}">
									<c:if test="${requestScope.gLBalance.status=='N'}">
										<button class="ladda-button ladda-button-demo btn btn-success btn-lg" data-style="expand-right">&nbsp;&nbsp;保存&nbsp;&nbsp;<i class="fa fa-save"></i></button>
									</c:if>
								</c:if>
							</div>
						</div>
						
						<input type="hidden" name="balanceId" value="${requestScope.gLBalance.balanceId}">
						<input type="hidden" name="createdDate" value="${requestScope.gLBalance.createdDate}">
						<input type="hidden" name="createdBy" value="${requestScope.gLBalance.createdBy}">
					</form>
				</div>
			</div>
		</div>
	</div>
</div>

<div id="glSubjectDiv"></div>

<script>
	$(document).ready(function() {
		var globalSubmitFlag = "Y";
		
		//初始化ledgerCode
		if("${requestScope.gLBalance.ledgerCode}"!=""){
			$("#ledgerCode").val("${requestScope.gLBalance.ledgerCode}");
		}
		//初始化currencyCode
		if("${requestScope.gLBalance.currencyCode}"!=""){
			$("#currencyCode").val("${requestScope.gLBalance.currencyCode}");
		}
		//初始化periodName
		if("${requestScope.gLBalance.periodName}"!=""){
			$("#periodName").val("${requestScope.gLBalance.periodName}");
		}
		
		$("#balanceCode").prop("readonly", true);
		
		//科目选择框
		$("input[name='subjectDesc']").focus(function(){
			selectSubjectInput = $(this);
			initCOA($(this));
		});
		
		//初始化获取分类账信息
		initLedgerCode();
		
		//异步获取分类账信息
		$("#ledgerCode").change(function(){
			initLedgerCode();
		});
		
		function initLedgerCode(){
			if($("#ledgerCode").val()!=""){
				$.ajax({
					type: "post",
					url: "web/gLLedger/getGLLedgerInfoAjax",
					data: {"ledgerCode": $("#ledgerCode").val()},
					async: false,
					dataType: "json",
					cache: false,
					success: function(data){
						if(data!=""){
							$("#currencyCode").val(data.currencyCode);
							if(data.startupType=="YEAR_BEGIN"){
								$("#startupTypeText").text("账簿启用时间：年初启用");
								$("#beginDr").prop("disabled", true);
								$("#beginCr").prop("disabled", true);
							}else if(data.startupType=="YEAR_MIDDLE"){
								$("#startupTypeText").text("账簿启用时间：年中启用");
								$("#beginDr").prop("disabled", false);
								$("#beginCr").prop("disabled", false);
							}
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						redragonJS.alert(textStatus);
					}
				});
			}
		}
		
		//当科目变更时验证是否存在
		$("input[name='subjectCode']").on("change", function() {
			if($("#ledgerCode").val()==""){
				redragonJS.alert("请先选择分类账簿");
			}else if($(this).val()!=""){
				$.ajax({
					type: "post",
					url: "web/gLBalance/isExistBeginBalanceAjax",
					data: {"ledgerCode": $("#ledgerCode").val(), "subjectCode": $(this).val()},
					async: false,
					dataType: "html",
					cache: false,
					success: function(data){
						if(data=="Y"){
							globalSubmitFlag = "N";
							$("#subjectCodeSpan").show();
						}else{
							globalSubmitFlag = "Y";
							$("#subjectCodeSpan").hide();
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						redragonJS.alert(errorThrown);
					}
				});
			}
        });
		
		
		//表单提交
		var l = $('.ladda-button-demo').ladda();

		l.click(function() {
			$("#editForm").valid();
			//l.ladda('stop');
		});

		$("#editForm").validate({
			rules : {
				balanceCode : {
					required : true,
				},
				ledgerCode : {
					required : true,
				},
				currencyCode : {
					required : true,
				},
				periodName : {
					required : true,
				},
				subjectCode : {
					required : true,
				},
			},
			submitHandler: function(form) {
				var submitFlag = "Y";
				
				if(globalSubmitFlag=="N"){
					redragonJS.alert("当前科目结构已录入期初，不能重复录入");
					submitFlag = "N";
					return false;
				}
				
				//验证字段
				if($("#beginDr").val()==""&&$("#beginCr").val()==""){
					redragonJS.alert("期初借方金额和贷方金额不能都为空");
					submitFlag = "N";
					return false;
				}else if(parseFloat($("#beginDr").val())>0&&parseFloat($("#beginCr").val())>0){
					redragonJS.alert("期初借方金额或贷方金额只能填写一个");
					submitFlag = "N";
					return false;
				}else if(parseFloat($("#beginDr").val())<0||parseFloat($("#beginCr").val())<0){
					redragonJS.alert("期初借方金额或贷方金额不能小于0");
					submitFlag = "N";
					return false;
				}
				
				if($("#endDr").val()==""&&$("#endCr").val()==""){
					redragonJS.alert("年初借方金额和贷方金额不能都为空");
					submitFlag = "N";
					return false;
				}else if(parseFloat($("#endDr").val())>0&&parseFloat($("#endCr").val())>0){
					redragonJS.alert("年初借方金额或贷方金额只能填写一个");
					submitFlag = "N";
					return false;
				}else if(parseFloat($("#endDr").val())<0||parseFloat($("#endCr").val())<0){
					redragonJS.alert("年初借方金额或贷方金额不能小于0");
					submitFlag = "N";
					return false;
				}
				
				if(parseFloat($("#beginDr").val())==0&&parseFloat($("#beginCr").val())==0
						&&parseFloat($("#endDr").val())==0&&parseFloat($("#endCr").val())==0){
					redragonJS.alert("借方金额和贷方金额不能都为0");
					submitFlag = "N";
					return false;
				}
				
				if(submitFlag=="Y"){
					l.ladda('start');
		        	form.submit();
				}
		    }
		});
	});
	
	//初始化COA
	function initCOA(obj){
		var subjectCode = $(obj).parent().find("input[name='subjectCode']").val();
		$.ajax({
			type: "post",
			url: "web/gLCodeCombinations/initCOA",
			data: {"subjectCode": subjectCode},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#glSubjectDiv").html(data);
					$('#coaDiv').modal('show');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(errorThrown);
			}
		});
	}
</script>